﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PServiceBus.Core.Runtime {
    /// <summary>
    /// Class for handling exception throw from message handler
    /// </summary>
    public class HandleErrorArgs<TMessage> : EventArgs where TMessage : class {
        private Exception _error;
        public HandleErrorArgs(Exception error, IList<TMessage> messages) {
            _error = error;
            Messages = messages;
            Continue = false;
        }
        /// <summary>
        /// Exception throw within a message handler
        /// </summary>
        public Exception Error { get { return _error; } }

        /// <summary>
        /// Contains raw string representation of message before been deserialized into <typeparam name="TMessage"></typeparam>
        /// </summary>
        public string RawMessage { get; set; }

        /// <summary>
        /// Messages being processed when error occured
        /// </summary>
        public IList<TMessage> Messages { get; set; }

        /// <summary>
        /// Returns true if messages was been processed when error occured
        /// </summary>
        public bool HasMessages {
            get {
                return Messages != null && Messages.Count > 0;
            }
        }

        /// <summary>
        /// Returns true if only raw message was successfully retrieved
        /// </summary>
        public bool HasRawMessage {
            get {
                return !String.IsNullOrWhiteSpace(RawMessage);
            }
        }

        /// <summary>
        /// Continue the handler if set to true
        /// </summary>
        public bool Continue { get; set; }
    }
}
